package com.eiss.service.impl;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

import com.eiss.common.core.domain.model.LoginUser;
import com.eiss.common.utils.SecurityUtils;
import com.eiss.constants.RedisConstants;
import com.eiss.domain.DTO.DeliverystationsDTO;
import com.eiss.domain.VO.DeliverystationsVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.geo.Distance;
import org.springframework.data.geo.Point;
import org.springframework.data.redis.connection.RedisGeoCommands;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import com.eiss.mapper.DeliverystationsMapper;
import com.eiss.domain.Entily.Deliverystations;
import com.eiss.service.IDeliverystationsService;

/**
 * 站点管理Service业务层处理
 * 
 * @author gzh
 * @date 2025-01-18
 */
@Service
@Slf4j
public class DeliverystationsServiceImpl implements IDeliverystationsService 
{
    @Autowired
    private DeliverystationsMapper deliverystationsMapper;
    @Autowired
    private RedisTemplate redisTemplate;
    /**
     * 查询站点管理
     * 
     * @param StationID 站点管理主键
     * @return 站点管理
     */
    @Override
    public DeliverystationsDTO selectDeliverystationsByStationID(Long StationID)
    {
        return deliverystationsMapper.selectDeliverystationsByStationID(StationID);
    }

    /**
     * 查询站点管理列表
     * 
     * @param deliverystations 站点管理
     * @return 站点管理
     */
    @Override
    public List<DeliverystationsDTO> selectDeliverystationsList(Deliverystations deliverystations)
    {
        List<DeliverystationsDTO> ls=deliverystationsMapper.selectDeliverystationsList(deliverystations);
        log.info("结果:{}",ls);
        return ls;
    }

    /**
     * 新增站点管理
     * 
     * @param deliverystationsVO 站点管理
     * @return 结果
     */
    @Override
    public int insertDeliverystations(DeliverystationsVO deliverystationsVO)
    {
        Deliverystations deliverystations=new Deliverystations();
        Deliverystations builder = deliverystations.builder()
                .StationName(deliverystationsVO.getStationName())
                .Address(deliverystationsVO.getAddress())
                .ContactPhone(deliverystationsVO.getContactPhone())
                .ManagerId(deliverystationsVO.getManagerId())
                .PostalCode(deliverystationsVO.getPostalCode())
                .X(deliverystationsVO.getX())
                .Y(deliverystationsVO.getY())
                .Province(deliverystationsVO.getProvince())
                .City(deliverystationsVO.getCity())
                .createTime(String.valueOf(LocalDateTime.now()))
                .updateTime(String.valueOf(LocalDateTime.now()))
                .status(1)
                .build();
        int i = deliverystationsMapper.insertDeliverystations(builder);
        Point point = new Point(Double.valueOf(builder.getX()), Double.valueOf(builder.getY()));
        redisTemplate.opsForGeo().add(RedisConstants.REDIS_EISS_STATION,point,builder.getStationID());
        return i;
    }

    /**
     * 修改站点管理
     * 
     * @param deliverystationsVO 站点管理
     * @return 结果
     */
    @Override
    public int updateDeliverystations(DeliverystationsVO deliverystationsVO)
    {
        Deliverystations deliverystations=new Deliverystations();
        log.info("前端传入数据:{}", deliverystations);
        Deliverystations builder = deliverystations.builder()
                .StationName(deliverystationsVO.getStationName())
                .Address(deliverystationsVO.getAddress())
                .ContactPhone(deliverystationsVO.getContactPhone())
                .ManagerId(deliverystationsVO.getManagerId())
                .PostalCode(deliverystationsVO.getPostalCode())
                .X(deliverystationsVO.getX())
                .Y(deliverystationsVO.getY())
                .Province(deliverystationsVO.getProvince())
                .City(deliverystationsVO.getCity())
                .updateTime(String.valueOf(LocalDateTime.now()))
                .StationID(deliverystationsVO.getStationID())
                .build();
        //删除原有的key
        redisTemplate.opsForZSet().remove(RedisConstants.REDIS_EISS_STATION,builder.getStationID());
        //添加修改之后的key
        int i = deliverystationsMapper.updateDeliverystations(builder);
        Point point = new Point(Double.valueOf(builder.getX()), Double.valueOf(builder.getY()));
        redisTemplate.opsForGeo().add(RedisConstants.REDIS_EISS_STATION,point,builder.getStationID());
        return i;
    }

    /**
     * 批量删除站点管理
     * 
     * @param StationIDs 需要删除的站点管理主键
     * @return 结果
     */
    @Override
    public int deleteDeliverystationsByStationIDs(Long[] StationIDs)
    {
        for (Long StationID : StationIDs) {
            redisTemplate.opsForZSet().remove(RedisConstants.REDIS_EISS_STATION,StationID);
        }
        return deliverystationsMapper.deleteDeliverystationsByStationIDs(StationIDs);
    }

    /**
     * 删除站点管理信息
     * 
     * @param StationID 站点管理主键
     * @return 结果
     */
    @Override
    public int deleteDeliverystationsByStationID(Long StationID)
    {
        redisTemplate.opsForZSet().remove(RedisConstants.REDIS_EISS_STATION,StationID);
        return deliverystationsMapper.deleteDeliverystationsByStationID(StationID);
    }

    @Override
    public List<DeliverystationsDTO> Nearbystation(Double x, Double y) {
        List<DeliverystationsDTO> lis=deliverystationsMapper.selectDeliverystationsList(null);
        LoginUser loginUser = SecurityUtils.getLoginUser();
        log.info("测试数据:{}",lis);
        Long userId=null;
        if(loginUser!=null){
            userId = loginUser.getUserId();
        }
        List<DeliverystationsDTO> pl=new ArrayList<>();
        Point p=new Point(x,y);
        redisTemplate.opsForGeo().add(RedisConstants.REDIS_EISS_STATION,p,RedisConstants.REDIS_EISS_USERIP+userId);
        for(int j=0;j<lis.size();j++){
            DeliverystationsDTO deliverystationsDTO = lis.get(j);
            Long i=deliverystationsDTO.getStationID();
            Distance distance = redisTemplate.opsForGeo().distance(RedisConstants.REDIS_EISS_STATION, RedisConstants.REDIS_EISS_USERIP + userId, i, RedisGeoCommands.DistanceUnit.KILOMETERS);
            log.info("位置:{}",distance.getValue());
            if(distance.getValue()<=100.00){
                pl.add(deliverystationsDTO);
            }
        }
        redisTemplate.opsForZSet().remove(RedisConstants.REDIS_EISS_STATION,RedisConstants.REDIS_EISS_USERIP+userId);
        return pl;
    }


}
